package com.barneyx.mshinfoplatform.web.front.user;

import com.barneyx.mshinfoplatform.common.ExResult;
import com.barneyx.mshinfoplatform.exception.UserException;
import com.barneyx.mshinfoplatform.util.UserUtil;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

@RestController
@RequestMapping("/api/front/user/wechat/")
public class WeChatController {
    @Resource
    private UserUtil userUtil;

    @GetMapping("oauth")
    public ExResult<?> H5Oauth(@Valid @NotBlank(message = "Code不能为空") @RequestParam("code") String code) throws UserException {
        return ExResult.ofAuto(userUtil.getUserInfoByWeChatCode(code));
    }

    @GetMapping("is-subscribe-mp")
    public ExResult<?> isSubscribeMp(@RequestParam("openid") String openId) throws WxErrorException {
        boolean userSubscribe = userUtil.getUserSubscribe(openId);
        return ExResult.ofSuccess(userSubscribe);
    }

    @GetMapping("mp-js-create-signature")
    public ExResult<?> mpJsCreateSignature(@RequestParam("url") String url) {
        return ExResult.ofAuto(userUtil.JsCreateSignature(url));
    }
}
